//
//  FSTitleView.m
//  fanshu
//
//  Created by mh on 16/3/27.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSTitleView.h"
#import "FSButton.h"
@interface FSTitleView ()
@property(nonatomic,weak)UIView *view;
@property(nonatomic,weak)UIView *downView;
@property(nonatomic,assign)titleViewBtnType seleted;
@property(nonatomic,strong)FSButton *seletedBtn;
@end
@implementation FSTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //初始化子控件
        //分割线
        UIView *view = [[UIView alloc] init];
        [self addSubview:view];
        view.backgroundColor = [UIColor lightGrayColor];
        self.view = view;
        
        //下划线
        UIView *downView = [[UIView alloc] init];
        [self addSubview:downView];
        downView.backgroundColor = [UIColor colorWithRed:191/255.0 green:123/255.0 blue:111/255.0 alpha:1.0f];
        self.downView = downView;
        self.seleted = 0;
        
        FSButton *firstTitle = [[FSButton alloc] init];
        [self addSubview:firstTitle];
        [firstTitle addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [firstTitle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [firstTitle setTitleColor:[UIColor colorWithRed:188/255.0 green:116/255.0 blue:115/255.0 alpha:1.0f] forState:UIControlStateSelected];
        firstTitle.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        firstTitle.tag = titleViewBtnTypeArticle;
        self.firstTitle = firstTitle;
        
        FSButton *secondTitle = [[FSButton alloc] init];
        [self addSubview:secondTitle];
        [secondTitle addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [secondTitle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [secondTitle setTitleColor:[UIColor colorWithRed:188/255.0 green:116/255.0 blue:115/255.0 alpha:1.0f] forState:UIControlStateSelected];
        secondTitle.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        secondTitle.tag = titleViewBtnTypeSpecialTopic;
        self.secondTitle = secondTitle;
    }
    [self btnOnClick:(FSButton *)self.firstTitle];
    return self;
}

- (void)showSelectedFirst:(BOOL)b{
    if (b) {
        [self btnOnClick:self.firstTitle];
    }else{
        [self btnOnClick:self.secondTitle];
    }
}

- (void)btnOnClick:(FSButton *)btn{
    self.seletedBtn.selected = NO;
    btn.selected = YES;
    self.seletedBtn = btn;
    self.seleted = btn.tag;
    [self setNeedsLayout];
    //通知代理，按钮被点击
    if ([self.delegate respondsToSelector:@selector(titleViewOnClick:btn:)]) {
        [self.delegate titleViewOnClick:self btn:btn];
    }
}


- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat viewX = self.width * 0.5;
    CGFloat viewW = 1;
    CGFloat viewH = self.height * 0.5;
    CGFloat viewY = (self.height - viewH) * 0.5;
    self.view.frame = CGRectMake(viewX, viewY, viewW, viewH);
    
    //设置位置尺寸
    CGFloat firstTitleX = 0;
    CGFloat firstTitleY = 0;
    CGFloat firstTitleW = self.width * 0.5;
    CGFloat firstTitleH = self.height;
    self.firstTitle.frame = CGRectMake(firstTitleX, firstTitleY,firstTitleW,firstTitleH);
    
    CGFloat secondTitleX = CGRectGetMaxX(self.firstTitle.frame);
    CGFloat secondTitleY = firstTitleY;
    CGFloat secondTitleW = firstTitleW;
    CGFloat secondTitleH = firstTitleH;
    self.secondTitle.frame = CGRectMake(secondTitleX, secondTitleY, secondTitleW, secondTitleH);

    CGFloat downViewX;
    CGFloat downViewW = self.width * 0.5;
    CGFloat downViewH = 3;
    CGFloat downViewY = CGRectGetMaxY(self.frame) - downViewH;
    if (self.seleted == titleViewBtnTypeArticle) {
        downViewX = firstTitleX;
    }else{
        downViewX = secondTitleX;
    }
    [UIView animateWithDuration:0.15f animations:^{
        self.downView.frame = CGRectMake(downViewX, downViewY, downViewW, downViewH);
    }];
}

@end
